package com.healthyeating.manage.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.healthyeating.common.core.domain.entity.SysUser;
import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.common.utils.ServletUtils;
import com.healthyeating.common.utils.ip.AddressUtils;
import com.healthyeating.common.utils.ip.IpUtils;
import com.healthyeating.manage.domain.UserShare;
import com.healthyeating.manage.domain.vo.CommentVo;
import com.healthyeating.manage.domain.vo.ShareVo;
import com.healthyeating.manage.mapper.CommentMapper;
import com.healthyeating.manage.service.ICommentService;
import com.healthyeating.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.ShareMapper;
import com.healthyeating.manage.domain.Share;
import com.healthyeating.manage.service.IShareService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;

/**
 * 发现分享Service业务层处理
 *
 * @author 段奇乐
 * @date 2025-03-22
 */
@Slf4j
@Service
public class ShareServiceImpl implements IShareService {

    @Autowired
    private ShareMapper shareMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询发现分享
     *
     * @param shareId 发现分享主键
     * @return 发现分享
     */
    @Override
    public Share selectShareByShareId(Long shareId) {
        return shareMapper.selectShareByShareId(shareId);
    }

    /**
     * 查询发现分享列表
     *
     * @param share 发现分享
     * @return 发现分享
     */
    @Override
    public List<ShareVo> selectShareList(Share share) {
        List<Share> shareList = shareMapper.selectShareList(share);
        return shareList.stream().map(_share -> {
            ShareVo shareVo = new ShareVo();
            BeanUtil.copyProperties(_share, shareVo);
            SysUser user = userMapper.selectUserById(_share.getUserId());
            shareVo.setAvatar(user.getAvatar());
            shareVo.setUserName(user.getUserName());
            UserShare userShare = shareMapper.selectUserShareByShareIdAndUserId(_share.getShareId(), SecurityUtils.getUserId());
            if (userShare != null) {
                shareVo.setShareFlag(userShare.getShareFlag());
            } else {
                shareVo.setShareFlag(0);
            }
            return shareVo;
        }).collect(Collectors.toList());
    }

    /**
     * 新增发现分享
     *
     * @param share 发现分享
     * @return 结果
     */
    @Override
    @Transactional
    public int insertShare(Share share, HttpServletRequest request) {
        share.setUserId(SecurityUtils.getUserId());
        share.setCreateTime(DateUtils.getNowDate());
        String ipRegion = AddressUtils.getRealAddressByIP(IpUtils.getIpAddr(ServletUtils.getRequest()));
        share.setCreateAddr(ipRegion);
        share.setUpdateTime(DateUtils.getNowDate());
        share.setUpdateUser(SecurityUtils.getUserId());
        share.setThumbNum(0);
        int result = shareMapper.insertShare(share);
        // 新增点赞记录（未点赞）
        UserShare userShare = new UserShare();
        userShare.setUserId(SecurityUtils.getUserId());
        userShare.setShareId(share.getShareId());
        userShare.setShareFlag(0);
        shareMapper.insertUserShare(userShare);
        return result;
    }

    /**
     * 修改发现分享
     *
     * @param shareVo 发现分享
     * @return 结果
     */
    @Override
    @Transactional
    public int updateShare(ShareVo shareVo) {
        shareVo.setUpdateTime(DateUtils.getNowDate());
        UserShare userShare = shareMapper.selectUserShareByShareIdAndUserId(shareVo.getShareId(), SecurityUtils.getUserId());
        if (userShare == null) {
            UserShare _userShare = new UserShare();
            _userShare.setUserId(SecurityUtils.getUserId());
            _userShare.setShareId(shareVo.getShareId());
            _userShare.setShareFlag(1);
            shareMapper.insertUserShare(_userShare);
        } else {
            userShare.setShareFlag( shareVo.getShareFlag() == 1 ? 0 : 1);
            shareMapper.updateUserShare(userShare);
        }
        return shareMapper.updateShare(shareVo);
    }

    /**
     * 批量删除发现分享
     *
     * @param shareIds 需要删除的发现分享主键
     * @return 结果
     */
    @Override
    public int deleteShareByShareIds(Long[] shareIds) {
        return shareMapper.deleteShareByShareIds(shareIds);
    }

    /**
     * 删除发现分享信息
     *
     * @param shareId 发现分享主键
     * @return 结果
     */
    @Override
    public int deleteShareByShareId(Long shareId) {
        return shareMapper.deleteShareByShareId(shareId);
    }
}
